package com.hualan.sfzy.service.course.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.entity.Course;
import com.hualan.sfzy.entity.CourseVideo;
import com.hualan.sfzy.mapper.course.CourseMapper;
import com.hualan.sfzy.mapper.course.CourseVideoMapper;
import com.hualan.sfzy.service.course.CourseVideoService;
import com.hualan.sfzy.utils.TimeConverter;
import com.hualan.sfzy.utils.VideoDurationExtractor;
import com.hualan.sfzy.vo.CourseVideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class CourseVideoServiceImpl implements CourseVideoService {
    @Autowired
    private CourseVideoMapper courseVideoMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public int add(CourseVideo entity) {
        return courseVideoMapper.insert(entity);
    }

    @Override
    public int add(CourseVideoVo entity) {

        String url = entity.getUrl();
        String diskPath = url.replace("http://localhost:8080/upload", "D:/upload");
        int time = (int)VideoDurationExtractor.getVideoDuration(url);
        System.out.println(time);
        entity.setTime(time);

        Course course = courseMapper.selectById(entity.getCourseId());
        course.setTime(course.getTime()+time);
        course.setVideoCount(course.getVideoCount()+1);
        courseMapper.update(course);

        return courseVideoMapper.insertVo(entity);
    }

    @Override
    public int edit(CourseVideo entity) {
        return courseVideoMapper.update(entity);
    }

    @Override
    public int edit(CourseVideoVo entity) {
        String url = entity.getUrl();
        String diskPath = url.replace("http://localhost:8080/upload", "D:/upload");
        int time = (int)VideoDurationExtractor.getVideoDuration(url);
        System.out.println(time);
        entity.setTime(time);
        CourseVideo courseVideo = courseVideoMapper.selectById(entity.getId());
        int oldTime = courseVideo.getTime();
        Course course = courseMapper.selectById(entity.getCourseId());
        course.setTime(course.getTime()+time-oldTime);

        courseMapper.update(course);

        return courseVideoMapper.updateVo(entity);
    }

    @Override
    public int remove(Integer id) {
        CourseVideo courseVideo = courseVideoMapper.selectById(id);
        int time = courseVideo.getTime();
        Course course = courseMapper.selectById(courseVideo.getCourseId());
        course.setTime(course.getTime()-time);
        course.setVideoCount(course.getVideoCount()-1);
        courseMapper.update(course);
        return courseVideoMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        for (Integer id: ids
             ) {
            CourseVideo courseVideo = courseVideoMapper.selectById(id);
            int time = courseVideo.getTime();
            Course course = courseMapper.selectById(courseVideo.getCourseId());
            course.setTime(course.getTime()-time);
            course.setVideoCount(course.getVideoCount()-1);
            courseMapper.update(course);
        }
        return courseVideoMapper.deleteBatch(ids);
    }

    @Override
    public List<CourseVideo> find() {
        return courseVideoMapper.selectAll();
    }

    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseVideo entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseVideo> courseVideos = courseVideoMapper.select(entity);
        PageInfo<CourseVideo> pageInfo = new PageInfo<>(courseVideos);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }
    @Override
    public Map<String, Object> findPage(Integer pageNum, Integer pageSize, CourseVideoVo entity) {
        PageHelper.startPage(pageNum,pageSize);
        List<CourseVideoVo> courseVideoVos = courseVideoMapper.selectVo(entity);
        for (CourseVideoVo courseVideoVo: courseVideoVos
             ) {
            courseVideoVo.setTimeStr(TimeConverter.toMinSecStr(courseVideoVo.getTime()));
        }
        PageInfo<CourseVideoVo> pageInfo = new PageInfo<>(courseVideoVos);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        result.put("pageNum", pageInfo.getPageNum());
        result.put("pageSize", pageInfo.getPageSize());
        result.put("pages", pageInfo.getPages());
        return result;
    }

    @Override
    public CourseVideo findById(Integer id) {
        return courseVideoMapper.selectById(id);
    }
}
